package com.qs.eggs.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.CollAdapter;
import com.qs.eggs.adapter.CollectAdapter;
import com.qs.eggs.adapter.FooAdapter;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.bean.CollectBean;
import com.qs.eggs.refresh.widget.RecyclerRefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class Collect1Activity extends Activity implements View.OnClickListener ,Runnable{
    public static final int ACTION_EDIT=1;
    public static final int ACTION_CAMPLATE=2;
    private RecyclerView recyclerView;
    private CollectAdapter collectAdapter;
    private TextView mEdit;
    protected RecyclerRefreshLayout mRefreshLayout;
    protected boolean isRefreshing;
    private ImageView fanhui;
    public static Activity instance;
    private Handler mHandler;
    List<Map<String,Object>> collectlist=null;
    private CollAdapter collAdapter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance=this;
        Thread thread = new Thread(this);
        thread.start();
        setContentView(R.layout.activity_collect1);
        initView();
        getData();
    }
    private  void initView(){
        recyclerView=(RecyclerView)findViewById(R.id.collect_recyclerview);
        fanhui = (ImageView) findViewById(R.id.fanhui);
        mEdit=(TextView)findViewById(R.id.tv_edit);
        mEdit.setOnClickListener(this);
        //mEdit.setTag(ACTION_EDIT);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collect1Activity.this.finish();
            }
        });
    }
  private void getData(){
      OkGo.post(Constants.URL_BASE+"collect?action=searchCollection")
              .execute(new StringCallback() {
                  @Override
                  public void onSuccess(String s, Call call, Response response) {

                      try {
                          collectlist=new ArrayList<>();
                          JSONArray jsonArray = new JSONArray(s);
                          for (int i = 0; i < jsonArray.length(); i++) {
                              JSONObject jsonObject = jsonArray.getJSONObject(i);
                              Map<String, Object> map = new HashMap<>();
                              map.put("collectid", jsonObject.getString("collectid"));
                              map.put("type", jsonObject.getString("type"));
                              map.put("proid", jsonObject.getString("proid"));
                              map.put("proname", jsonObject.getString("proname"));
                              map.put("saleprice", jsonObject.getString("saleprice"));
                              map.put("autoname", jsonObject.getString("autoname"));
                              map.put("count", jsonObject.getString("count"));
                              collectlist.add(map);

                          }

                          collAdapter = new CollAdapter(Collect1Activity.this, collectlist,mHandler);
                          recyclerView.setAdapter(collAdapter);


                      }catch(Exception e){
                          e.printStackTrace();
                      }

                  }
              });

  }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_edit:
                Collect1Activity.this.finish();
        }
    }
        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler(){
                public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                        case 5:
                            getData();
                            break;
                        default:
                            break;
                    }
                }
            };
            Looper.loop();
        }
//        int action=(int)v.getTag();
//        if(collectAdapter.getDatas().size()==0){
//            Toast.makeText(CollectActivity.this,"暂无收藏",Toast.LENGTH_SHORT).show();
//          return;
//        }
//        switch (action){
//            case ACTION_EDIT:
//                for (int i = 0; i < recyclerView.getChildCount(); i++) {
//                    View view = recyclerView.getChildAt(i);
//                    ImageView img = (ImageView) view.findViewById(
//                            R.id.img_edit);
//                    img.setImageResource(R.mipmap.icon_del);
//                }
//                showDelControl();
//                collectAdapter.setEdit(ACTION_EDIT);
//                break;
//            case ACTION_CAMPLATE:
//                for (int i = 0; i < recyclerView.getChildCount(); i++) {
//                    View view = recyclerView.getChildAt(i);
//                    ImageView img = (ImageView) view.findViewById(
//                            R.id.img_edit);
//                    img.setImageResource(R.mipmap.icon_cart);
//                }
//                hideDelControl();
//                collectAdapter.setEdit(ACTION_CAMPLATE);
//                break;
//        }
    }

//    private void showDelControl(){
//        mEdit.setText("完成");
//        mEdit.setTag(ACTION_CAMPLATE);
//
//    }
//
//    private void  hideDelControl(){
//
//        mEdit.setText("编辑");
//        mEdit.setTag(ACTION_EDIT);
//
//    }

